import { BadRequestException, Controller, Query } from '@nestjs/common';
import { ArticleContentService } from './article-content.service';
import { ApiBearerAuth, ApiBody, ApiExcludeController, ApiExcludeEndpoint, ApiOperation, ApiProperty, ApiTags } from '@nestjs/swagger';
import { Body, Delete, Get, Param, Post, Put } from '@nestjs/common';
import { ArticleBindCategoryDto, CreateArticleContentDto, SimpleCreateArticleContentDto, SimpleUpdateArticleContentDto, UpdateArticleRawHtmlDto, ScienceKnowledgePageDto } from './article-content.dto';
import { AuthUser } from '~/modules/auth/decorators/auth-user.decorator';
import { AuthTenant } from '~/modules/tenant/tenant.decorator';
import { ErrorEnum } from '~/constants/error-code.constant';
import { ModuleSwaggerTags } from '~/constants/modules.constant';
import { Public } from '~/modules/auth/decorators/public.decorator';
import { StandardPagination } from '~/helper/paginate';
import { ArticleContentEntity } from './article-content.entity';

@ApiTags(ModuleSwaggerTags.ARTICLE)
@ApiBearerAuth()
@Controller('cms/article/content')
export class ArticleContentController {
  constructor(private readonly articleContentService: ArticleContentService) {}

  @Post()
  @ApiOperation({ summary: '创建文章内容' })
  @ApiBody({ type: CreateArticleContentDto })
  async create(
    @Body() dto: CreateArticleContentDto,
    @AuthUser() user: IAuthUser,
    @AuthTenant() tenant: IAuthTenant
  ) {
    return await this.articleContentService.create(dto);
  }

	@Post('simple')
	@ApiOperation({ summary: '简单创建文章内容' })
	@ApiBody({ type: SimpleCreateArticleContentDto })
	async simpleCreate(@Body() dto: SimpleCreateArticleContentDto) {
		return await this.articleContentService.simpleCreate(dto);
	}

	@Put('simple/:id')
	@ApiOperation({ summary: '简单更新文章内容' })
	@ApiBody({ type: SimpleUpdateArticleContentDto })
	async simpleUpdate(@Param('id') id: number, @Body() dto: SimpleUpdateArticleContentDto) {
		return await this.articleContentService.simpleUpdate(id, dto);
	}

	@Post('bind/category')
	@ApiOperation({ summary: '绑定文章分类' })
	@ApiBody({ type: ArticleBindCategoryDto })
	async bindCategory(@Body() dto: ArticleBindCategoryDto): Promise<void> {
		await this.articleContentService.bindCategory(dto);
	}

  @Post('update/rawhtml')
  @ApiOperation({ summary: '更新文章HTML内容' })
  @ApiBody({ type: UpdateArticleRawHtmlDto })
  async updateRawHtml(@Body() dto: UpdateArticleRawHtmlDto): Promise<void> {
    await this.articleContentService.update(dto.id, { rawHtml: dto.rawHtml });
  }

	@ApiExcludeEndpoint()
  @Get()
	@ApiOperation({ summary: '查询所有文章内容 - 待完成' })
  async findAll() {
    return await this.articleContentService.findAll();
  }

	@ApiExcludeEndpoint()
  @Get(':id')
	@ApiOperation({ summary: '查询文章内容 - 待完成' })
  async findOne(@Param('id') id: number) {
    return await this.articleContentService.findOne(id);
  }

	@ApiExcludeEndpoint()
  @Put(':id')
	@ApiOperation({ summary: '更新文章内容 - 待完成' })
  async update(@Param('id') id: number, @Body() dto: CreateArticleContentDto) {
    return await this.articleContentService.update(id, dto);
  }

  @Delete(':id')
	@ApiOperation({ summary: '删除文章内容 - 待完成' })
  async remove(@Param('id') id: number) {
    return await this.articleContentService.remove(id);
  }

	@ApiOperation({ summary: '通过编码和分类ID查询文章内容' })
	@Get('code/:code/category/:categoryId')
	@ApiOperation({ summary: '通过编码和分类ID查询文章内容' })
	async findByCodeAndCategory(@Param('code') code: string, @Param('categoryId') categoryId: number) {
		return await this.articleContentService.findByCodeAndCategory(code, categoryId);
	}

	@Post('create')
	@ApiOperation({ summary: '创建文章内容接口' })
	@ApiBody({ type: SimpleCreateArticleContentDto })
	async createArticle(@Body() dto: SimpleCreateArticleContentDto) {
		return await this.articleContentService.simpleCreate(dto);
	}

	@Put('update/:id')
	@ApiOperation({ summary: '更新文章内容接口' })
	@ApiBody({ type: SimpleUpdateArticleContentDto })
	async updateArticle(@Param('id') id: number, @Body() dto: SimpleUpdateArticleContentDto) {
		return await this.articleContentService.simpleUpdate(id, dto);
	}

	@Get('recommand/page')
	@ApiOperation({ summary: '分页查询科普知识文章' })
	async findScienceKnowledgePage(@Query() dto: ScienceKnowledgePageDto): Promise<StandardPagination<ArticleContentEntity>> {
		return await this.articleContentService.findScienceKnowledgePage(dto);
	}
}
